home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_4.9 / profile / profile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  6.0 KB  |  212 lines

  1. /* 
  2.  * profile.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* PROFILE:     program calculates horizontal or vertical profile of image
  10.  *                    usage: profile inimg outimg [-h || -v] [-s SIZE] [-L]
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "tiffimage.h"          /* picfile info on images */
  17. #include "images.h"
  18. extern void print_sos_lic ();
  19.  
  20. #define ON 0                    /* binarization values */
  21. #define OFF 255
  22. #define PROFILE_DFLT 0          /* =0 for horizontal; =1 for vertical prof. */
  23. #define PROFILE_HEIGHT 200      /* height of profile for display */
  24.  
  25. long usage (short);
  26. long input (int, char **, short *, long *, short *);
  27.  
  28.  
  29. main (argc, argv)
  30.      int argc;
  31.      char *argv[];
  32. {
  33.   Image *imgI, *imgO;           /* input and output image structures */
  34.   unsigned char **imgIn, **imgOut;  /* input and output images */
  35.   long *profile;                /* horizontal or vertical profile vector */
  36.   long widthI, heightI;         /* image size */
  37.   short horizOrVert;            /* flag =0 for horiz; = 1 for vert. profile */
  38.   long profLength;              /* length of profile */
  39.   long profHeight;              /* height of profile for display */
  40.   short printFlag;              /* to print out profile data */
  41.   long sum, maxSum;             /* sum and max. sum of profile values */
  42.   long x, y;
  43.  
  44. /* user input */
  45.   if (input (argc, argv, &horizOrVert, &profHeight, &printFlag) < 0)
  46.     return (-1);
  47.  
  48.   imgI = ImageIn (argv[1]);
  49.   imgIn = imgI->img;
  50.   heightI = ImageGetHeight (imgI);
  51.   widthI = ImageGetWidth (imgI);
  52.   printf ("image size is %dx%d\n", widthI, heightI);
  53.  
  54. /* determine horizontal or vertical profile */
  55.  
  56.   if (horizOrVert == 0) {       /* horizontal profile */
  57.     profLength = widthI;
  58.     imgO = ImageAlloc (profHeight, profLength, 8);
  59.     imgOut = ImageGetPtr (imgO);
  60.     for (y = 0; y < profHeight; y++)
  61.       for (x = 0; x < profLength; x++)
  62.         imgOut[y][x] = 255;
  63.  
  64.     if ((profile = (long *) calloc (profLength, sizeof (long))) == NULL) {
  65.       printf ("PROFILE: not enough memory -- sorry.\n");
  66.       return (-1);
  67.     }
  68.  
  69.     for (x = 0, maxSum = 0; x < widthI; x++) {
  70.       for (y = 0, sum = 0; y < heightI; y++) {
  71.         if (imgIn[y][x] == ON)
  72.           sum++;
  73.       }
  74.       profile[x] = sum;
  75.       if (printFlag != 0)
  76.         printf ("%ld\n", sum);
  77.       if (sum > maxSum)
  78.         maxSum = sum;
  79.     }
  80.   }
  81.  
  82.   else {                        /* vertical profile */
  83.     profLength = heightI;
  84.     imgO = ImageAlloc (profLength, profHeight, 8);
  85.     imgOut = ImageGetPtr (imgO);
  86.     for (y = 0; y < profLength; y++)
  87.       for (x = 0; x < profHeight; x++)
  88.         imgOut[y][x] = 255;
  89.  
  90.     if ((profile = (long *) calloc (profLength, sizeof (long))) == NULL) {
  91.       printf ("PROFILE: not enough memory -- sorry.\n");
  92.       return (-1);
  93.     }
  94.  
  95.     for (y = 0, maxSum = 0; y < heightI; y++) {
  96.       for (x = 0, sum = 0; x < widthI; x++) {
  97.         if (imgIn[y][x] == ON)
  98.           sum++;
  99.       }
  100.       profile[y] = sum;
  101.       if (printFlag != 0)
  102.         printf ("%ld\n", sum);
  103.       if (sum > maxSum)
  104.         maxSum = sum;
  105.     }
  106.   }
  107.  
  108. /* determine output image profile */
  109.   for (x = 0; x < profLength; x++)
  110.     profile[x] = (long) ((double) profile[x] *
  111.                          (double) profHeight / (double) maxSum + 0.5);
  112.  
  113.   if (horizOrVert == 0) {       /* horizontal profile */
  114.     for (x = 0; x < profLength; x++)
  115.       for (y = profHeight - profile[x]; y < profHeight; y++)
  116.         imgOut[y][x] = ON;
  117.   }
  118.   else {                        /* vertical profile */
  119.     for (y = 0; y < profLength; y++)
  120.       for (x = 0; x < profile[y]; x++)
  121.         imgOut[y][x] = ON;
  122.   }
  123.  
  124.   ImageOut (argv[2], imgO);
  125.  
  126.   return (0);
  127. }
  128.  
  129.  
  130.  
  131.  
  132. /* USAGE:       function gives instructions on usage of program
  133.  *                    usage: usage (flag)
  134.  *              When flag is 1, the long message is given, 0 gives short.
  135.  */
  136.  
  137. long
  138. usage (flag)
  139.      short flag;                /* flag =1 for long message; =0 for short message */
  140. {
  141.  
  142. /* print short usage message or long */
  143.   printf ("USAGE: profile inimg outimg [-h || -v] [-s SIZE] [-p] [-L]\n");
  144.   if (flag == 0)
  145.     return (-1);
  146.  
  147.   printf ("\nprofile determines the horizontal or vertical profile\n");
  148.   printf ("of a binary image, that is the summation of ON pixel values along\n");
  149.   printf ("the y-axis or the x-axis respectively; default is horizontal.\n\n");
  150.   printf ("ARGUMENTS:\n");
  151.   printf ("    inimg: input image filename (TIF)\n");
  152.   printf ("   outimg: output image filename (TIF)\n\n");
  153.   printf ("OPTIONS:\n");
  154.   printf ("  [-h || -v]: horizontal or vertical profile respectively.\n");
  155.   printf ("     -s SIZE: height of horizontal profile or width of vertical\n");
  156.   printf ("              profile (default = %d)\n", PROFILE_HEIGHT);
  157.   printf ("          -p: PRINT DATA FLAG if set, prints profile heights.\n");
  158.   printf ("          -L: print Software License for this module\n");
  159.  
  160.   return (-1);
  161. }
  162.  
  163.  
  164. /* INPUT:       function reads input parameters
  165.  *                  usage: input (argc, argv, &horizOrVert, &profHeight)
  166.  */
  167.  
  168. #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
  169.  
  170. long
  171. input (argc, argv, horizOrVert, profHeight, printFlag)
  172.      int argc;
  173.      char *argv[];
  174.      short *horizOrVert;        /* flag =0 for horizontal or =1 for vertical */
  175.      long *profHeight;          /* height of profile for display */
  176.      short *printFlag;          /* if =1, prints profile data; otherwise no */
  177. {
  178.   long n;
  179.  
  180.   if (argc == 1)
  181.     USAGE_EXIT (1);
  182.   if (argc == 2)
  183.     USAGE_EXIT (0);
  184.  
  185.   *horizOrVert = PROFILE_DFLT;
  186.   *profHeight = PROFILE_HEIGHT;
  187.   *printFlag = 0;
  188.  
  189.   for (n = 3; n < argc; n++) {
  190.     if (strcmp (argv[n], "-h") == 0)
  191.       *horizOrVert = 0;
  192.     else if (strcmp (argv[n], "-v") == 0)
  193.       *horizOrVert = 1;
  194.     else if (strcmp (argv[n], "-s") == 0) {
  195.       if (++n == argc)
  196.         USAGE_EXIT (0);
  197.       *profHeight = atol (argv[n]);
  198.     }
  199.     else if (strcmp (argv[n], "-p") == 0) {
  200.       *printFlag = 1;
  201.     }
  202.     else if (strcmp (argv[n], "-L") == 0) {
  203.       print_sos_lic ();
  204.       exit (0);
  205.     }
  206.     else
  207.       USAGE_EXIT (0);
  208.   }
  209.  
  210.   return (0);
  211. }
  212.